﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Settings;
using System.Threading;
using Utils;

namespace PanasonicWarranty.Business
{
    public partial class SmsMtWaiting
    {
        private static void BulkInsert(DataTable source)
        {
            if(source == null || source.Rows.Count == 0)
                return;
            try
            {
                using (SqlBulkCopy bc1 = new SqlBulkCopy(AppSetting.ConnectionString),
                                   bc2 = new SqlBulkCopy(AppSetting.ConnectionString),
                                   bc3 = new SqlBulkCopy(AppSetting.ConnectionString))
                {
                    //Insert mtReport
                    bc3.BatchSize = 1000;
                    bc3.BulkCopyTimeout = 0;
                    bc3.DestinationTableName = "SmsMtReport";
                    DataTable dtmtReport = source.DefaultView.ToTable(true, "MtGuid", "SentTime", "Status");
                    dtmtReport.Columns.Add("ReportTime", typeof(DateTime));
                    dtmtReport.Columns.Add("ReportStatus", typeof(string));
                    bc3.WriteToServer(dtmtReport);
                    //Insert mtStatistic
                    bc2.BatchSize = 1000;
                    bc2.BulkCopyTimeout = 0;
                    bc2.DestinationTableName = "SmsMtStatistic";
                    bc2.WriteToServer(source);
                    //Insert MtWaiting
                    bc1.BatchSize = 1000;
                    bc1.BulkCopyTimeout = 0;
                    bc1.DestinationTableName = "SmsMtWaiting";
                    bc1.WriteToServer(source);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void Insert(string shortCode, string message, int? agencyId, params string[] phonenumber)
        {
            try
            {
                DataTable dtSource = ObjectClass.CreateTableSchema<SmsMtWaiting>();
                string mttype = SmsShortCode.GetMtType(shortCode);
                foreach (var item in phonenumber)
                {
                    int operator_id = OperatorPrefixCode.GetOperatorID(item);
                    if (OperatorPrefixCode.IsMobilePhone(operator_id))
                    {
                        DataRow newRow = dtSource.NewRow();
                        dtSource.Rows.Add(newRow);
                        newRow["MtGuid"] = Guid.NewGuid();
                        newRow["MoGuid"] = DBNull.Value;
                        newRow["ShortCode"] = shortCode;
                        newRow["DestAddr"] = item;
                        newRow["OperatorID"] = operator_id;
                        newRow["Message"] = message;
                        newRow["CooperateID"] = 1;
                        newRow["Status"] = "PENDING";
                        newRow["SentTime"] = DateTime.Now;
                        newRow["CDRIndicator"] = "FREE";
                        newRow["MTType"] = mttype;
                        newRow["RetryOnFailReqTimes"] = 0;
                        newRow["RetryOnTimeOutReqTimes"] = 0;
                        newRow["RetryOnFailReqDuration"] = 0;
                        newRow["RetryOnTimeOutReqDuration"] = 0;
                        newRow["AgencyID"] = agencyId ?? (object)DBNull.Value;
                    }
                }
                BulkInsert(dtSource);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void Insert(string shortCode, string message, int? agencyId, List<string> phoneList)
        {
            try
            {
                DataTable dtSource = ObjectClass.CreateTableSchema<SmsMtWaiting>();
                string mttype = SmsShortCode.GetMtType(shortCode);
                foreach (var item in phoneList)
                {
                    int operator_id = OperatorPrefixCode.GetOperatorID(item);
                    if (OperatorPrefixCode.IsMobilePhone(operator_id))
                    {
                        DataRow newRow = dtSource.NewRow();
                        dtSource.Rows.Add(newRow);
                        newRow["MtGuid"] = Guid.NewGuid();
                        newRow["MoGuid"] = DBNull.Value;
                        newRow["ShortCode"] = shortCode;
                        newRow["DestAddr"] = item;
                        newRow["OperatorID"] = operator_id;
                        newRow["Message"] = message;
                        newRow["CooperateID"] = 1;
                        newRow["Status"] = "PENDING";
                        newRow["SentTime"] = DateTime.Now;
                        newRow["CDRIndicator"] = "FREE";
                        newRow["MTType"] = mttype;
                        newRow["RetryOnFailReqTimes"] = 0;
                        newRow["RetryOnTimeOutReqTimes"] = 0;
                        newRow["RetryOnFailReqDuration"] = 0;
                        newRow["RetryOnTimeOutReqDuration"] = 0;
                        newRow["AgencyID"] = agencyId ?? (object)DBNull.Value;
                    }
                }
                BulkInsert(dtSource);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
